home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ainet / t4runtim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-22  |  14.7 KB  |  409 lines

  1. /* ----------------------------------------------------------------------- *
  2.  *                                                                         *
  3.  *    (C) Copyright 1997 by:  aiNet                                        *
  4.  *                                        Trubarjeva 42                                *
  5.  *                            SI-3000 Celje                                *
  6.  *                                        Europe, Slovenia                             *
  7.  *     All Rights Reserved                                                 *
  8.  *                                                                         *
  9.  *     Subject: C code for a hole in the square problem.                   *
  10.  *     File:    T4RUNTIM - Hole int the square - dynamic binding           *
  11.  *                                                                         *
  12.  * ----------------------------------------------------------------------- */
  13.  
  14. /*
  15.  * This file assumes that ainetxx.dll will be loaded at run time,
  16.  * which is default option and no flags need to be defined.
  17.  * ainetxx.lib must NOT be included in the linking process.
  18.  */
  19. #include "ainetdll.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <conio.h> /* This is Borland specific header.      */
  25.                    /* It is used for getch() function only. */
  26.  
  27. /*
  28.  * Path and the filename of dll which will be loaded.
  29.  */
  30.  
  31. #if defined(__WIN32__)
  32. const char ainetDll[] = "ainet32.dll";
  33. #else
  34. const char ainetDll[] = "ainet16.dll";
  35. #endif
  36. /*
  37.  * Pointers to ainet dll functions. They are made global - all functions
  38.  * can use them.
  39.  */
  40.  
  41. t_aiRegistration              aiRegistration;
  42. t_aiGetVersion                aiGetVersion;
  43. t_aiCreateModel               aiCreateModel;
  44. t_aiCreateModelFromCSVFile    aiCreateModelFromCSVFile;
  45. t_aiDeleteModel               aiDeleteModel;
  46. t_aiNormalize                 aiNormalize;
  47. t_aiDenormalize               aiDenormalize;
  48. t_aiPrediction                aiPrediction;
  49. t_aiGetNumberOfVariables      aiGetNumberOfVariables;
  50. t_aiGetNumberOfModelVectors   aiGetNumberOfModelVectors;
  51. t_aiGetNumberOfInputVariables aiGetNumberOfInputVariables;
  52. t_aiSetDiscreteFlag           aiSetDiscreteFlag;
  53. t_aiGetDiscreteFlag           aiGetDiscreteFlag;
  54. t_aiSetVariable               aiSetVariable;
  55. t_aiGetVariable               aiGetVariable;
  56. t_aiGetVariableVB             aiGetVariableVB;
  57. t_aiGetCSVFileModelSize       aiGetCSVFileModelSize;
  58. // New in version 1.24
  59. t_aiSetCapacity                    aiSetCapacity;
  60. t_aiGetCapacity                    aiGetCapacity;
  61. t_aiGetFreeEntries                aiGetFreeEntries;
  62. t_aiInsertModelVector            aiInsertModelVector;
  63. t_aiOverwriteModelVector        aiOverwriteModelVector;
  64. t_aiAppendModelVector            aiAppendModelVector;
  65. t_aiDeleteModelVector            aiDeleteModelVector;
  66. t_aiPredictionEx                    aiPredictionEx;
  67. t_aiExcludeModelVector            aiExcludeModelVector;
  68. t_aiExcludeModelVectorRange    aiExcludeModelVectorRange;
  69. t_aiIsModelVectorExcluded        aiIsModelVectorExcluded;
  70. t_aiSaveCSVFile                    aiSaveCSVFile;
  71.  
  72.  
  73. /*
  74.  *  ainet32.dll module variable.
  75.  */
  76.  
  77. HINSTANCE hLib;
  78.  
  79. /*
  80.  *  The load_aiNetLibrary() function is implemented below.
  81.  *  This function will load ainet32.dll and define pointers to
  82.  *  ainet functions.
  83.  */
  84.  
  85. int load_aiNetLibrary(void);
  86.  
  87. /*
  88.  * #########################################################################
  89.  */
  90.  
  91. void main()
  92. {
  93.     /*
  94.      * Please, see the manual (Part 1, Chapter 2.3) for more details about a
  95.      * hole in the square problem.
  96.      */
  97.  
  98.     int i;
  99.    int counter;
  100.    int loop;
  101.     int version;
  102.    int outcome;
  103.     aiModel* model;
  104.    float x,y;
  105.    int mvIndex[5];
  106.  
  107.    /* Let us define 6 vectors to be predicted.
  108.     * This definitely not enough for a good test set. However, this
  109.     * is just an example how to use aiNet.DLL library. You can
  110.     * construct a larger if you like.
  111.  
  112.     * Test points were selected near the 'border'. One half of the set
  113.     * lies slightly inside - in the hole, and the other half of the
  114.     * set lies slightly outside - in the square.
  115.     *       +-------+--------+-------+-------+--------+
  116.     *       |   Fi  |   R    |   X   |   Y   | IN/OUT |
  117.     *       +-------+--------+-------+-------+--------+
  118.     *    1  |    0  |  0.75  | 0.750 | 0.000 |  OUT   |
  119.     *    2  |   60  |  0.65  | 0.325 | 0.563 |  IN    |
  120.     *    3  |  120  |  0.75  |-0.375 | 0.650 |  OUT   |
  121.     *    4  |  180  |  0.65  |-0.650 | 0.000 |  IN    |
  122.     *    5  |  240  |  0.75  |-0.375 |-0.650 |  OUT   |
  123.     *    6  |  300  |  0.65  | 0.325 |-0.563 |  IN    |
  124.     *       +-------+--------+-------+-------+--------+
  125.     */
  126.     float predict[6][3] = { { 0.750, 0.000, 999 },  /* vectors to be predicted */
  127.                                     { 0.325, 0.563, 999 },
  128.                                     {-0.375, 0.650, 999 },
  129.                                     {-0.650, 0.000, 999 },
  130.                                     {-0.375,-0.650, 999 },
  131.                                     { 0.325,-0.563, 999 } };
  132.    /*
  133.     * Load DLL
  134.     */
  135.    if( !load_aiNetLibrary() ) {
  136.         exit(EXIT_FAILURE);
  137.     }
  138.    
  139.     /*
  140.      * Print out the title
  141.      */
  142.     version = aiGetVersion();
  143.     printf( "\naiNetDLL version %i.%i! (C) Copyright by aiNet, 1997",
  144.               version/100, version%100 );
  145.     printf( "\n---------------------------------------------------\n" );
  146.  
  147.     /*
  148.      * Register DLL
  149.      */
  150.     aiRegistration( "Your registration name", "Your code" );
  151.  
  152.     /*
  153.      * Setup the model. We will start with 10 model vectors. They will be
  154.     * generated using random number generator.
  155.      */
  156.     model = aiCreateModel(10,3,2);
  157.     if(!model) {
  158.         printf( "\nError: Something went wrong during model creation!" );
  159.         exit(EXIT_FAILURE);
  160.     }
  161.  
  162.    /* Let us generate initial model vectors */
  163.    randomize();
  164.    for(i=1; i<=10; i++) {
  165.        float inside;
  166.        x = (float)((random(2000)-1000))/1000.0;
  167.        y = (float)((random(2000)-1000))/1000.0;
  168.  
  169.       if ( (x*x + y*y - 0.490) <= 0.0 )
  170.           inside = 1.0;
  171.        else
  172.           inside = 0.0;
  173.  
  174.       aiSetVariable(model,i,1,x);
  175.       aiSetVariable(model,i,2,y);
  176.       aiSetVariable(model,i,3,inside);
  177.    }
  178.  
  179.     /*
  180.      * Output the model
  181.      */
  182.  
  183.     printf( "\n             Model name: aiNet DLL test 4 (Hole in the square)" );
  184.     printf( "\nNumber of model vectors: %i", aiGetNumberOfModelVectors(model));
  185.     printf( "\n    Number of variables: %i", aiGetNumberOfVariables(model));
  186.     printf( "\n         Variable names: X,   Y,   IN(1)/OUT(0)" );
  187.     printf( "\n          Discrete flag: %i,   %i,   %i",
  188.               aiGetDiscreteFlag(model,1),
  189.               aiGetDiscreteFlag(model,2),
  190.               aiGetDiscreteFlag(model,3) );
  191.     for( i=1; i<=aiGetNumberOfModelVectors(model); i++ ) {
  192.         printf( "\n\t\t\t % 5.3f, % 5.3f, % 5.3f",
  193.                   aiGetVariable(model, i,1),
  194.                   aiGetVariable(model, i,2),
  195.                   aiGetVariable(model, i,3) );
  196.     }
  197.  
  198.     /* This loop will be repeated 7 times                        */
  199.    /* Each time new model vectors will be added to the problem. */
  200.  
  201.     for(loop=0; loop<7; loop++) {
  202.        int oldSize;
  203.         /*
  204.          * Normalize the model
  205.          */
  206.         aiNormalize(model,NORMALIZE_REGULAR);
  207.  
  208.         /*
  209.          * Prediction: Pen. coefficient = 0.30, Pen. method = STATIC
  210.          * This test has static penalty coefficient 0.30
  211.          */
  212.  
  213.         printf( "\n\n  Penalty coefficient: 0.15" );
  214.         printf(   "\n       Penalty method: STATIC" );
  215.         printf(   "\n\t X(inp), Y(inp), IN/OUT (out)" );
  216.         counter = 0;
  217.         for ( i=0; i<6; i++ ) {
  218.           int inside;
  219.          int j;
  220.          /* TRUE stands for most influent MV */
  221.             aiPredictionEx( model, predict[i], 0.15, PENALTY_STATIC, mvIndex, 5, TRUE );
  222.             printf( "\n    % 7.4f, % 7.4f, % 7.4f",
  223.                       predict[i][0],predict[i][1],predict[i][2] );
  224.          inside = predict[i][2] >= 0.5;
  225.           if( inside )
  226.               printf( " IN " );
  227.           else
  228.               printf( " OUT" );
  229.           if( inside == i%2 ) {
  230.               printf( " PASSED" );
  231.              counter++;
  232.           }
  233.           else {
  234.               printf( " FAILED" );
  235.           }
  236.          printf( " Most inf. MVs: " );
  237.          for( j=0; j<5; j++ ) {
  238.              printf( "%i", mvIndex[j] );
  239.             if(j!=4) printf(", ");
  240.          }
  241.         }
  242.        printf( "\n\t%i Correct predictions (%3.2f)!", counter, counter/6.0*100.0 );
  243.       printf( "\nPress any key to continue!" );
  244.        getch();
  245.  
  246.         /*
  247.          * Denormalize the model!
  248.         * We must do that, because new model vectors will be added to the model.
  249.          */
  250.         aiDenormalize(model);
  251.  
  252.       /*
  253.        * Each time, 100 new model vectors will be added to the model
  254.        */
  255.         oldSize = aiGetCapacity(model);
  256.         outcome = aiSetCapacity(model,oldSize+100);
  257.       if(outcome < 0) {
  258.             printf( "\nError: Something went wrong during capacity call!" );
  259.             exit(EXIT_FAILURE);
  260.       }
  261.  
  262.       for(i=0; i<100; i++) {
  263.           float newModelVector[3];
  264.            float inside;
  265.            x = (float)((random(2000)-1000))/1000.0;
  266.            y = (float)((random(2000)-1000))/1000.0;
  267.  
  268.           if ( (x*x + y*y - 0.490) <= 0.0 )
  269.               inside = 1.0;
  270.            else
  271.               inside = 0.0;
  272.  
  273.           newModelVector[0] = x;
  274.           newModelVector[1] = y;
  275.           newModelVector[2] = inside;
  276.          outcome = aiAppendModelVector(model,newModelVector);
  277.           if(outcome < 0) {
  278.                 printf( "\nError: Appending new model vector failure!" );
  279.                 exit(EXIT_FAILURE);
  280.           }
  281.       }
  282.    }
  283.  
  284.     /*
  285.     * Finally we will use other new functions. There is no particular
  286.     * reason for their use. We will use them for demonstration purposes only!
  287.     */
  288.  
  289.     printf( "\n" );
  290.     printf( "\nNumber of model vectors in the model: %i",
  291.                 aiGetNumberOfModelVectors(model));
  292.     printf( "\nCapacity of the model: %i",
  293.                 aiGetCapacity(model));
  294.     printf( "\nFree entries in the model: %i",
  295.                 aiGetFreeEntries(model));
  296.  
  297.     /*
  298.     * Before we can save the model, the model must be denormalized in order
  299.     * to restore initial model values.
  300.     */
  301.  
  302.     aiSaveCSVFile(model,"Test.csv");
  303.     /*
  304.      * We must call the aiDeleteModel function here since the model
  305.      * was allocated dynamicaly using the aiCreateModelFromCSVFile function.
  306.      */
  307.     aiDeleteModel(model);
  308.  
  309.    model = aiCreateModelFromCSVFile("Test.csv");
  310.    aiDeleteModel(model);
  311.  
  312.     printf( "\n\nEnd." );
  313.    getch();
  314.     exit(EXIT_SUCCESS);
  315. }
  316.  
  317. /*
  318.  *  This function binds AINET.DLL with function prototypes.
  319.  */
  320.  
  321. int load_aiNetLibrary()
  322. {
  323.    /*
  324.     * Load the Dynamic Link Library AINET32.DLL
  325.     */
  326.  
  327.    hLib = LoadLibrary(ainetDll);
  328.    if((unsigned)hLib<=HINSTANCE_ERROR){
  329.       char bfr[40];
  330.       wsprintf(bfr, "Failure loading library: %s", ainetDll);
  331.       MessageBox(NULL, bfr, "Error", MB_OK|MB_APPLMODAL);
  332.       return 0;
  333.    }
  334.  
  335.    /*
  336.     * Get all the entry points for the functions in ainet32.dll
  337.     */
  338.  
  339.     aiRegistration              = (t_aiRegistration)              GetProcAddress(hLib, "aiRegistration");
  340.    aiGetVersion                = (t_aiGetVersion)                GetProcAddress(hLib, "aiGetVersion");
  341.    aiCreateModel               = (t_aiCreateModel)               GetProcAddress(hLib, "aiCreateModel");
  342.    aiCreateModelFromCSVFile    = (t_aiCreateModelFromCSVFile)    GetProcAddress(hLib, "aiCreateModelFromCSVFile");
  343.    aiDeleteModel               = (t_aiDeleteModel)               GetProcAddress(hLib, "aiDeleteModel");
  344.    aiNormalize                 = (t_aiNormalize)                 GetProcAddress(hLib, "aiNormalize");
  345.    aiDenormalize               = (t_aiDenormalize)               GetProcAddress(hLib, "aiDenormalize");
  346.    aiPrediction                = (t_aiPrediction)                GetProcAddress(hLib, "aiPrediction");
  347.    aiGetNumberOfVariables      = (t_aiGetNumberOfVariables)      GetProcAddress(hLib, "aiGetNumberOfVariables");
  348.    aiGetNumberOfModelVectors   = (t_aiGetNumberOfModelVectors)   GetProcAddress(hLib, "aiGetNumberOfModelVectors");
  349.    aiGetNumberOfInputVariables = (t_aiGetNumberOfInputVariables) GetProcAddress(hLib, "aiGetNumberOfInputVariables");
  350.    aiSetDiscreteFlag           = (t_aiSetDiscreteFlag)           GetProcAddress(hLib, "aiSetDiscreteFlag");
  351.    aiGetDiscreteFlag           = (t_aiGetDiscreteFlag)           GetProcAddress(hLib, "aiGetDiscreteFlag");
  352.    aiSetVariable               = (t_aiSetVariable)               GetProcAddress(hLib, "aiSetVariable");
  353.    aiGetVariable               = (t_aiGetVariable)               GetProcAddress(hLib, "aiGetVariable");
  354.    aiGetVariableVB             = (t_aiGetVariableVB)             GetProcAddress(hLib, "aiGetVariableVB");
  355.    aiGetCSVFileModelSize       = (t_aiGetCSVFileModelSize)       GetProcAddress(hLib, "aiGetCSVFileModelSize");
  356.    aiSetCapacity               = (t_aiSetCapacity)               GetProcAddress(hLib, "aiSetCapacity");
  357.    aiGetCapacity               = (t_aiGetCapacity)               GetProcAddress(hLib, "aiGetCapacity");
  358.    aiGetFreeEntries            = (t_aiGetFreeEntries)            GetProcAddress(hLib, "aiGetFreeEntries");
  359.    aiInsertModelVector         = (t_aiInsertModelVector)         GetProcAddress(hLib, "aiInsertModelVector");
  360.    aiOverwriteModelVector      = (t_aiOverwriteModelVector)      GetProcAddress(hLib, "aiOverwriteModelVector");
  361.    aiAppendModelVector         = (t_aiAppendModelVector)         GetProcAddress(hLib, "aiAppendModelVector");
  362.    aiDeleteModelVector         = (t_aiDeleteModelVector)         GetProcAddress(hLib, "aiDeleteModelVector");
  363.    aiPredictionEx              = (t_aiPredictionEx)              GetProcAddress(hLib, "aiPredictionEx");
  364.    aiExcludeModelVector        = (t_aiExcludeModelVector)        GetProcAddress(hLib, "aiExcludeModelVector");
  365.    aiExcludeModelVectorRange   = (t_aiExcludeModelVectorRange)   GetProcAddress(hLib, "aiExcludeModelVectorRange");
  366.    aiIsModelVectorExcluded     = (t_aiIsModelVectorExcluded)     GetProcAddress(hLib, "aiIsModelVectorExcluded");
  367.    aiSaveCSVFile               = (t_aiSaveCSVFile)               GetProcAddress(hLib, "aiSaveCSVFile");
  368.  
  369.    /*
  370.     * GetProcAddress returns null on failure
  371.     */
  372.    if( aiRegistration == NULL
  373.        || aiGetVersion == NULL
  374.        || aiCreateModel == NULL
  375.        || aiCreateModelFromCSVFile == NULL
  376.        || aiDeleteModel == NULL
  377.        || aiNormalize == NULL
  378.        || aiDenormalize == NULL
  379.        || aiPrediction == NULL
  380.        || aiGetNumberOfVariables == NULL
  381.        || aiGetNumberOfModelVectors == NULL
  382.        || aiGetNumberOfInputVariables == NULL
  383.        || aiSetDiscreteFlag == NULL
  384.        || aiGetDiscreteFlag == NULL
  385.        || aiSetVariable == NULL
  386.        || aiGetVariable == NULL
  387.        || aiGetVariableVB == NULL
  388.        || aiGetCSVFileModelSize == NULL
  389.          || aiSetCapacity == NULL
  390.        || aiGetCapacity == NULL
  391.        || aiGetFreeEntries == NULL
  392.        || aiInsertModelVector == NULL
  393.        || aiOverwriteModelVector == NULL
  394.        || aiAppendModelVector == NULL
  395.        || aiDeleteModelVector == NULL
  396.        || aiPredictionEx == NULL
  397.        || aiExcludeModelVector == NULL
  398.        || aiExcludeModelVectorRange == NULL
  399.        || aiIsModelVectorExcluded == NULL
  400.        || aiSaveCSVFile == NULL ) {
  401.         MessageBox(NULL, "Failure locating procedures.", "Error",
  402.             MB_OK|MB_APPLMODAL);
  403.       return 0;
  404.     }
  405.    return 1;
  406. }
  407.  
  408. /* THE END */
  409.